<%#
 Copyright 2013-2020 the original author or authors from the JHipster project.

 This file is part of the JHipster project, see https://www.jhipster.tech/
 for more information.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
-%>
import { browser, element, by } from 'protractor';

import NavBarPage from './../../<%= entityParentPathAddition %>page-objects/navbar-page';
import SignInPage from './../../<%= entityParentPathAddition %>page-objects/signin-page';
import <%= entityClass %>ComponentsPage from './<%= entityFileName %>.page-object';
<%_ if (!readOnly) { _%>
import <%= entityClass %>UpdatePage from './<%= entityFileName %>-update.page-object';
<%_ } _%>
import { waitUntilDisplayed, waitUntilAnyDisplayed, click, getRecordsCount, waitUntilHidden, waitUntilCount, isVisible } from '../../<%= entityParentPathAddition %>util/utils';
<%_ if (fieldsContainBlobOrImage && !readOnly) { _%>
import path from 'path';
<%_ } _%>
<%_
let openBlockComment = '';
let closeBlockComment = '';
for (let relationship of relationships) {
    if (relationship.relationshipRequired || relationship.useJPADerivedIdentifier) {
        openBlockComment = '/* ';
        closeBlockComment = ' */';
        break;
    }
} _%>

const expect = chai.expect;

describe('<%= entityClass %> e2e test', () => {
    let navBarPage: NavBarPage;
    let signInPage: SignInPage;
    let <%= entityInstance %>ComponentsPage: <%= entityClass %>ComponentsPage;
<%_ if (!readOnly) { _%>
    let <%= entityInstance %>UpdatePage: <%= entityClass %>UpdatePage;
<%_ } _%>

    before(async () => {
        await browser.get('/');
        navBarPage = new NavBarPage();
        signInPage = await navBarPage.getSignInPage();
        <%_ if (authenticationType !== 'oauth2') { _%>
        await signInPage.waitUntilDisplayed();

        await signInPage.username.sendKeys('admin');
        await signInPage.password.sendKeys('admin');
        await signInPage.loginButton.click();
        await signInPage.waitUntilHidden();
        <%_ } else { _%>
        await signInPage.loginWithOAuth('admin', 'admin');
        <%_ } _%>
        await waitUntilDisplayed(navBarPage.entityMenu);
        await waitUntilDisplayed(navBarPage.adminMenu);
        await waitUntilDisplayed(navBarPage.accountMenu);
    });

    beforeEach(async() => {
        await browser.get('/');
        await waitUntilDisplayed(navBarPage.entityMenu);
        <%= entityInstance %>ComponentsPage = new <%= entityClass %>ComponentsPage();
        <%= entityInstance %>ComponentsPage = await <%= entityInstance %>ComponentsPage.goToPage(navBarPage);
    });

    it('should load <%= entityClassPlural %>', async () => {
        expect(await <%= entityInstance %>ComponentsPage.title.getText())
            .to.match(/<%= entityClassPluralHumanized %>/);
        <%_ if (!readOnly) { _%>
        expect(await <%= entityInstance %>ComponentsPage.createButton.isEnabled()).to.be.true;
        <%_ } _%>
    });

<%_ if (!readOnly) { _%>
    <%= openBlockComment %>it('should create and delete <%= entityClassPlural %>', async () => {
        const beforeRecordsCount = await isVisible(<%= entityInstance %>ComponentsPage.noRecords) ? 0 : await getRecordsCount(<%= entityInstance %>ComponentsPage.table);
        <%= entityInstance %>UpdatePage = await <%= entityInstance %>ComponentsPage.goToCreate<%= entityClass %>();
        await <%= entityInstance %>UpdatePage.enterData();

        expect(await <%= entityInstance %>ComponentsPage.createButton.isEnabled()).to.be.true;
        await waitUntilDisplayed(<%= entityInstance %>ComponentsPage.table);
        await waitUntilCount(<%= entityInstance %>ComponentsPage.records, beforeRecordsCount + 1);
        expect(await <%= entityInstance %>ComponentsPage.records.count()).to.eq(beforeRecordsCount + 1);
        
        await <%= entityInstance %>ComponentsPage.delete<%= entityClass %>();
        if(beforeRecordsCount !== 0) { 
          await waitUntilCount(<%= entityInstance %>ComponentsPage.records, beforeRecordsCount);
          expect(await <%= entityInstance %>ComponentsPage.records.count()).to.eq(beforeRecordsCount);
        } else {
          await waitUntilDisplayed(<%= entityInstance %>ComponentsPage.noRecords);
        }
    });<%= closeBlockComment %>
<%_ } _%>

    after(async () => {
        await navBarPage.autoSignOut();
    });
});
